package com.maochd.cloud.file.controller;

import com.maochd.cloud.common.core.domain.R;
import com.maochd.cloud.file.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(value = "文件Controller", tags = "文件Controller")
@RestController
public class FileController {

    @Resource
    private FileService fileService;

    @ApiOperation(value = "上传", notes = "文件上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true),
            @ApiImplicitParam(name = "filename", value = "文件名称"),
    })
    @PostMapping("/upload")
    public R<String> upload(@RequestPart("file") MultipartFile file,
                            @RequestParam(value = "filename", required = false) String filename) {
        return R.ok(fileService.upload(file, filename));
    }

    @ApiOperation(value = "内部上传（feign）", notes = "内部上传（feign）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", required = true),
            @ApiImplicitParam(name = "filename", value = "文件名称"),
    })
    @PostMapping("/upload/inner")
    public R<String> uploadInputStream(@RequestPart("file") MultipartFile file,
                                       @RequestParam(value = "filename", required = false) String filename) {
        return R.ok(fileService.upload(file, filename));
    }
}
